package com.xiaoyi.encrypt;

import com.xiaoyi.annotataion.Encrypt;
import com.xiaoyi.config.EncryptionConfig;
import com.xiaoyi.util.AesUtil;
import com.xiaoyi.util.RSAUtil;
import com.xiaoyi.util.SM2Util;
import com.xiaoyi.util.SM4Util;

import java.util.Objects;

/**
 * 对称加密 能加能解密
 *
 * @author xiaoyi
 * @version 1.0.0
 * @date 2023/08/01 15:17:00
 */
public class RsaEncryptor implements IEncryptorStrategy {

    @Override
    public String encrypt(String value, EncryptionConfig config, Encrypt encrypt) {
        return RSAUtil.encrypt(value, getPublicPassword(config, encrypt));
    }

    @Override
    public String decrypt(String value, EncryptionConfig config, Encrypt encrypt) {
        return RSAUtil.decrypt(value, getPrivatePassword(config, encrypt));
    }

    private String getPublicPassword(EncryptionConfig config, Encrypt annotation) {
        if (Objects.nonNull(annotation.publicKey()) && !"".equals(annotation.publicKey())) {
            return annotation.publicKey();
        }
        return config.getPublicKey();
    }

    private String getPrivatePassword(EncryptionConfig config, Encrypt annotation) {
        if (Objects.nonNull(annotation.privateKey()) && !"".equals(annotation.privateKey())) {
            return annotation.privateKey();
        }
        return config.getPrivateKey();
    }
}
